package locksupport.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ZhuQi
 * @ClassName LockSupportDemo
 * @Date 2022/9/13 15:21
 * @Version 1.0
 * @Description 测试 Condition 的 await 和 notify
 */
public class LockSupportDemo {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Thread thread1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " lock");
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " await");
                condition.await();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            System.out.println(Thread.currentThread().getName() + " 唤醒");
        }, "thread1");

        Thread thread2 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " signal");
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "thread2");

        thread1.start();
        thread2.start();
    }



}
